package com.yyy.jdk.map;

import lombok.Builder;
import lombok.Data;
import org.junit.Test;

import java.util.ArrayList;
import java.util.IdentityHashMap;
import java.util.List;
import java.util.Map;

/**
 * Map测试
 *
 * @author yiyongyao
 * @date 2023/11/22 15:18
 **/
public class MapTests {
    /**
     * 非静态内部类不支持 @builder
     */
    @Data
    @Builder
    private static class Student {
        private String name;
        private Integer age;
    }

    @Test
    public void IdentityHashMapTest() {
        List<Student> list = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            Student student = Student.builder().age(i).name("第n号").build();
            list.add(student);
        }
        Map<String, Student> map = new IdentityHashMap<>();
        list.forEach(student -> {
            map.put(new String(student.getName()), student);  //必须要用new String()，否则key无法重复
        });
        map.forEach((k, v) -> {
            System.out.println(k + ":" + v);
        });
        System.out.println(map.get("第n号"));
    }
}
